package com.huike.clues.service.impl;


import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.common.constant.HttpStatus;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.bean.BeanUtils;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 线索跟进记录Service业务层处理
 */
@Service
public class TbClueTrackRecordServiceImpl implements ITbClueTrackRecordService {

    @Autowired
    private TbClueMapper clueMapper;
    @Autowired
    private TbClueTrackRecordMapper recordMapper;

    /**
     *  新增线索跟进记录
     * @param tbClueTrackRecord 线索跟进记录
     */
    @Override
    @SneakyThrows
    @Transactional(rollbackFor = Exception.class)
    public Object insertTrackRecord(ClueTrackRecordVo tbClueTrackRecord) {
        //获取用户名
        String username = SecurityUtils.getUsername();
        //获取创建时间
        Date now = new Date();
        tbClueTrackRecord.setCreateBy(username);
        tbClueTrackRecord.setCreateTime(now);
        if (tbClueTrackRecord.getType()==null){
            tbClueTrackRecord.setType("0");//0正常跟进  1 伪线索
        }

        //创建 TbClue对象
        TbClue tbClue = new TbClue();
        //设置忽略字段
        String[] strs =new String[]{"id","status","createBy","createTime","params"};
        //复制对象
        BeanUtils.copyProperties(tbClueTrackRecord, tbClue, strs);
        tbClue.setId(tbClueTrackRecord.getClueId());
        //修改线索表
        clueMapper.updateTbClue( tbClue);
        return recordMapper.insertTrackRecord(tbClueTrackRecord);
    }

    /**
     *查询线索跟进记录列表
     */
    @Override
    public List<TbClueTrackRecord> selectClueTrackRecords(Long clueId) {

        /* TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        int total = tbClueTrackRecords.size();

        rspData.setRows(tbClueTrackRecords);
        rspData.setTotal(total);*/
        return recordMapper.selectClueTrackRecords(clueId);
    }
}
